class Solution(object):
    def pancakeSort(self, arr):
        k = 1
        n = len(arr)
        out = []
        while k < n:
            for i in range(n - k, -1, -1):
                if arr[i] == k:
                    index = i
                    break
            if index != n - k:
                out.append(index + 1)
                arr[:index + 1] = arr[index::-1]
                out.append(n - k + 1)
                arr[:n - k + 1]= arr[n - k::-1]
            k += 1
        out.append(n)
        return out




data = Solution()
arr = [3, 2, 4, 1]
print(data.pancakeSort(arr))
